//
// Created by Jisam on 2024/6/18.
// 21点42分
#include <bits/stdc++.h>

using  namespace  std;
int a[6];
signed main (){
    int T;
    cin >> T;
    int sum = 0;
    while(T --){
        cin >> a[1]>> a[2]>> a[3]>> a[4]>> a[5];
        int sum = a[1] + a[2]+ a[3]+ a[4]+ a[5];
        int i5 = min(a[5],a[1]);
        a[5] -= i5 , a[1] -= i5;

        int i4 = min(a[4],a[2]);
        a[4] -= i4 , a[2] -= i4;

        int i3 = a[3] /2;
        i3 %= 2;

        sum -=  i5*2 + i4*2 + i3*2;

        int j5 = 0;
        if(a[5] > 0){
            for(int i = 2; i <= 5 ; i ++){
                if (a[i] > 0 && a[5] > 0){
                    int temp =  min( a[i],a[5]);
                    a[5] -= temp;
                    a[i] -= temp;
                    sum -= temp * 2;
                    j5 += temp;
                }
            }
        }
        int j4 = 0;
        if(a[4] > 0){
            for(int i = 1; i <= 4 ; i ++){
                if (a[i] > 1 && a[4] > 0){
                    int temp =  min( a[i] / 2,a[4]);
                    a[4] -= temp;
                    a[i] -= temp * 2;
                    sum -= temp * 3;
                    j4 += temp;
                }
            }
        }

        int j3 = 0;
        while(a[3] > 0){
            if(a[1] > 0 && a[2] > 0){
                int temp = min(a[1],a[2]);
                a[1] -= temp;
                a[2] -= temp;
                a[3] -= temp;
                sum -= temp * 3;
                j3 += temp;
            }
            if(a[1] >= 3){
                int temp = a[1] / 3;
                a[1] -= temp *3;
                a[3] -= temp;
                j3 += temp;
                sum -= temp * 4;
            }
            if(a[2] >= 2){
                int temp = a[2] / 2;
                a[2] -= temp *2;
                a[3] -= temp;
                j3 += temp;
                sum -= temp * 3;
            }
        }
        int j2 = 0;
        if(a[2] > 0){
            if(a[2] >= 3){
                int temp = a[2] / 3 ;
                a[2] -= temp * 3;
                j2 += temp;
                sum -= temp * 3;
            }
            if(a[1] >=4 ){
                int temp = a[1] / 4;
                a[1] -= temp * 4;
                a[2] -= temp;
                j2 += temp;
                sum -= temp * 5;
            }

        }
        int j1 = sum / 6;
        cout << i5 + i4 + i3 +j5 + j4 +j3 +j2 +j1 <<endl;

    }
    return  0;
}